#include "building.h"
#include"utils.h"

Building::Building(QPoint enterence, QPixmap buildingPixmap, QGraphicsScene *scene)
    : QGraphicsPixmapItem(buildingPixmap)
{
    scene->addItem(this);

    setAcceptHoverEvents(true);

    setData(ITEM_TYPE,GameItems::Building);

    overlayPixmap = buildingPixmap;
    overlayPixmap.fill(QColor(255,0,0,180));
    overlayPixmap.setAlphaChannel(pixmap());

    toolTip = new BuildingToolTip(scene);
    infectedPeople = 0;
    buildingHealth = 0;

    enterencePoint = enterence;
    sterility = 990;
    setZValue(2);

}

void Building::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    emit clicked(this);
}

void Building::addPerson(Person *newPerson)
{
    currentPeople.append(newPerson);
}

void Building::removePerson(Person *oldPerson)
{
    if (currentPeople.indexOf(oldPerson) > -1)
        currentPeople.removeAt(currentPeople.indexOf(oldPerson));
}

void Building::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    //QGraphicsPixmapItem::paint(painter,option,widget);

    // If the building is infected draw the red overlay
    if (((qreal)infectedPeople/currentPeople.length()) > 0.5)
    {
        painter->drawPixmap(0,0,overlayPixmap);
    }
}

void Building::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    toolTip->moveToolTip(event->scenePos());
    toolTip->show();
}

void Building::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    toolTip->hide();
}

void Building::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
    toolTip->moveToolTip(event->scenePos());
}

void Building::advance(int phase)
{

    if ((phase == 0))
    {
        int newHealth = 0, newInfected = 0;

        for (int index = 0; index < currentPeople.length(); index++)
        {
            newHealth += currentPeople.at(index)->stats->hpGetter();
            if (currentPeople.at(index)->virus != 0)
                newInfected++;
        }



        if (newHealth!=0)   // We have to do this so we don't get a divide by zero error with no people in the house
            buildingHealth = ((newHealth/currentPeople.length())/1000.0);
        infectedPeople = newInfected;

        toolTip->updateToolTip(buildingHealth, currentPeople.length(), infectedPeople);


        for(int i = currentPeople.count() - 1; i >= 0; i--)
        {
            for(int j = currentPeople.count() - 1; j >= 0; j--)
            {
                if((rand() % 1000) >= sterility &&
                   currentPeople.at(i)->GetInBuilding() && currentPeople.at(j)->GetInBuilding() && i != j)
                {
                    currentPeople.at(i)->collidesWithPerson(currentPeople.at((j)));
                }
            }
        }



    }
}

QList<Person*> Building::CurrentPeopleAccessor()
{
    return currentPeople;
}

QPoint Building::getEnterencePoint()
{
    return enterencePoint;
}

bool Building::isInfected()
{
    return (((qreal)infectedPeople/currentPeople.length()) > 0.5);
}

int Building::SterilityGetter()
{
    return sterility;
}

void Building::SterilitySetter(int value)
{
    sterility = value;
    sterility = Utils::clamp(sterility, 900, 999);
}
